package com.robooot.business_counter.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * 只保留日期
     */
    public static Date dateOnly(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return df.parse(df.format(date));
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static Date format(String str) {
        return format(str, null);
    }

    public static Date format(String str, String pattern) {
        if (pattern == null) {
            pattern = "yyyy-MM-dd";
        }
        DateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(str);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * 当前时间加上days天
     */
    public static Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    /**
     * 当前时间加上days月
     */
    public static Date addMonths(Date date, int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);
        return cal.getTime();
    }

    /**
     * 当前时间加上years年
     */
    public static Date addYears(Date date, int years) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, years);
        return cal.getTime();
    }

    /**
     * 计算两个日期的相隔天数
     */
    public static int daysBetween(Date early, Date late) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(early);
        long time1 = cal.getTimeInMillis();
        cal.setTime(late);
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * date转换格式字符串,默认格式yyyy-MM-dd
     *
     * @param date 时间
     * @return
     */
    public static String toString(Date date) {
        return getStrByDate(date, "yyyy-MM-dd");
    }

    /**
     * date转换格式字符串
     *
     * @param date   时间
     * @param parten 转换格式
     * @return
     */
    public static String getStrByDate(Date date, String parten) {
        SimpleDateFormat sdf = new SimpleDateFormat(parten);
        String result = "";
        if (date != null) result = sdf.format(date);
        return result;
    }

    public static String getStrByLocalDateTime(LocalDateTime localDateTime, String pattern) {
        Date date = DateUtil.localDateTime2Date(localDateTime);
        return DateUtil.getStrByDate(date,pattern);
    }

    public static String getStrByLocalDateTime(LocalDateTime localDateTime) {
        return DateUtil.getStrByLocalDateTime(localDateTime,"yyyy-MM-dd HH:mm:SS");
    }
    /**
     * 获取上周第一天
     * @return
     */

    public static Date getPreviousMonday() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.add(Calendar.DATE, -1 * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取上星期最后一天
     * @return
     */

    public static Date getSunday() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.add(Calendar.DATE, -1*7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取上月第一天
     * @return
     */

    public static Date getMonthFist(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static Date getMonthFist(){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获取两段时间间隔多少天
     * @param start
     * @param end
     * @return
     */
    public static int days(Date start,Date end) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            start=sdf.parse(sdf.format(start));
            end=sdf.parse(sdf.format(end));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        long time1 = cal.getTimeInMillis();
        cal.setTime(end);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }
    /**
     * 上月最后一天
     * @return
     */

    public static Date getMonthLast(){
        Calendar calendar=Calendar.getInstance();
        int month=calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month-1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    public static Date getYearLast() {
        Calendar cale =Calendar.getInstance();
        cale.add(Calendar.YEAR, 0);
        cale.set(Calendar.DAY_OF_YEAR, 1);
        return cale.getTime();
    }


    public static String getSdfTimes() {
        return sdfTimes.format(new Date());
    }

    public static Date localDate2Date(LocalDate localDate) {
        if(null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate date2LocalDate(Date date) {
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zone);
        LocalDate localDate = localDateTime.toLocalDate();
        return localDate;
    }

    public static LocalDateTime date2LocalDateTime(Date date) {
        LocalDateTime time = null;
        if (date != null) {
            Instant instant = date.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            time = instant.atZone(zoneId).toLocalDateTime();
        }
        return time;
    }
}
